﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.Quyen;
using DevExpress.XtraEditors.Repository;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmQuyen : DevExpress.XtraEditors.XtraForm
    {
        public frmQuyen()
        {
            InitializeComponent();
        }
        DataTable tbl;
        String errConnect;
        String errSQL;        

        private void frmQuyen_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }
        public void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            quyenctrl ctrl = new quyenctrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["ID_QUYEN"].Caption = "";
            GridView1.Columns["TEN_QUYEN"].Caption = "Tên Quyền";
            GridView1.Columns["TRANGTHAI"].Caption = "";
            GridView1.Columns["TRANGTHAI"].Visible = false;
            GridView1.Columns["NGAY_TAO"].Caption = "Ngày Tạo";           
            GridView1.Columns["ID_QUYEN"].Visible = false;

            // Đổi cột ngày tạo trong grid thành ItemDateEdit giống với datetimePicker

            RepositoryItemDateEdit riDate = new RepositoryItemDateEdit();
            riDate.ShowClear = true;
            riDate.ShowToday = true;
            riDate.EditMask = "dd/MM/yyyy";
            riDate.Mask.UseMaskAsDisplayFormat = true;
            GridView1.Columns["NGAY_TAO"].ColumnEdit = riDate;
            GridControl1.RepositoryItems.Add(riDate);
            GridView1.Columns["NGAY_TAO"].ColumnEdit = riDate;

            GridView1.BestFitColumns();
        }

        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            Library.ExportToExcel(this, "Danh Mục Quyền", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (GridView1.FocusedRowHandle >= 0)
            {
                int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_QUYEN"].FieldName));
                errConnect = "";
                errSQL = "";
                string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["TEN_QUYEN"].FieldName).ToString();
                Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa! " + strName + " ?");
                if (result == false)
                {
                    return;
                }
                quyenctrl ctrl = new quyenctrl();
                ctrl.Delete(ref errConnect, ref errSQL, id);
                if (errSQL != "")
                {
                    Library.ShowMessage("Không thể xóa quyền này");
                }
                else
                {
                    GridView1.DeleteSelectedRows();
                    GridView1.RefreshData();
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                quyenctrl ctrl = new quyenctrl();
                quyeninfo info = new quyeninfo();

                errConnect = "";
                errSQL = "";
                DataRow row = GridView1.GetDataRow(e.RowHandle);
                info.ID_QUYEN = Convert.ToInt32(RTValidate.CheckNull(row["ID_QUYEN"]));
                info.TEN_QUYEN = RTValidate.CheckNull(row["TEN_QUYEN"]).ToString();
                info.TRANGTHAI = 1;
                row["NGAY_TAO"] = DateTime.Now.ToString("dd/MM/yyyy");
                info.NGAY_TAO = Convert.ToDateTime(RTValidate.CheckNull(row["NGAY_TAO"]));
                if (info.ID_QUYEN != 0)
                {
                    Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi bạn có muốn tiếp tục");
                    if (result != false)
                    {
                        ctrl.Update(ref errConnect, ref errSQL, info);
                    }
                }
                else
                {
                    ctrl.Insert(ref errConnect, ref errSQL, info);
                }
                this.InitGrid();
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }
    }
}